package controllers

import (
	"fmt"
	"github.com/beego/beego/v2/core/logs"
	beego "github.com/beego/beego/v2/server/web"
	"go1/db"
)

// requestDump, err := httputil.DumpRequest(req, true)
//
//	if err != nil {
//	 fmt.Println(err)
//	}
//
// fmt.Println(string(requestDump))
type MainController struct {
	beego.Controller
}

//func (c MainController) PrintBody() {
//	requestbody, _ := io.ReadAll(c.Ctx.Request.Body)
//	log.Printf("requestbody: %v", string(requestbody))
//}

type User struct {
	Id     string `json:"id"`
	Name   string `json:"name"`
	Phone  string `json:"phone"`
	Gender int    `json:"gender"`
}

func (u User) GetTableName(opts any) string {
	return "users"
}

type UserDao struct {
	db.CbCrud[User]
}

func (c *MainController) Get() {
	c.Data["Website"] = "beego.vip"
	c.Data["Email"] = "astaxie@gmail.com"
	c.TplName = "index.tpl"
}

func (c *MainController) Test() {
	//requestDump, err := httputil.DumpRequest(c.Ctx.Request, true)
	//if err != nil {
	//	fmt.Println(err)
	//}
	//fmt.Println(string(requestDump))

	fMap := make(map[string]any)
	fMap["name"] = "张三"
	a := []any{
		[]any{"LIKE", "name", "%tom%"},
		[]any{">1", "id1", 1},
		[]any{">=", "id2", 2},
		[]any{"<", "id3", 3},
		[]any{"<=", "id4", 4},
		[]any{"name", "张三"},
		[]any{"user.name", "张三"},
		[]any{"age", 18},
		[]any{"name", "tom"},
		[]any{"sql", "(age >= $1 OR age <= $2)"},
		fMap,
	}

	opts := &db.CrudOpts{
		Params: []any{18, 22},
		Sql:    "returning *",
	}
	_ = opts
	_ = a
	dao := UserDao{}
	sql := dao.BuildFilter(a, opts)
	logs.Info(sql)
	c.JSONResp(ReplyOk())
}

func (c *MainController) Error() {
	logs.Info("test error222")
	testError()
	c.JSONResp(ReplyOk())
}

func testError() {
	panic(fmt.Sprintf("test  error, %v", 666))
}
